<?php
declare (strict_types = 1);

namespace app\api\controller\user;

use app\admin\model\user\UserSignDeploy;
use app\api\controller\Base;
use app\common\service\user\UserSign;
use think\facade\Cache;
use think\Request;
use app\admin\model\user\User;

class Sign extends Base
{
    /**
     * Author:Yingbo Sun
     * 签到
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function sign()
    {
        $uid = $this->request->uid;
        if (empty($uid)){
            $data = ['code' => 400,'message' => '参数错误'];
            return $this->res($data);
        }
        //签到
        $res = UserSign::sign($uid);
        //返回结果
        return $this->res($res);
    }

    /**
     * Author:Yingbo Sun
     * 补签
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function retroactive()
    {
        $uid = $this->request->uid;
        if (empty($uid)){
            $data = ['code' => 400,'message' => '参数错误'];
            return $this->res($data);
        }
        //补签
        $res = UserSign::retroactive($uid);
        return  $this->res($res);
    }

    /**
     * Author:Yingbo Sun
     * 查看当天是否签到
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function isSign()
    {
        $uid = $this->request->uid;
        if (empty($uid)){
            $data = ['code' => 400,'message' => '参数错误'];
            return $this->res($data);
        }
        //查询当天是否签到
        $res = UserSign::isSign($uid);
        return $this->res($res);
    }

    /**
     * Author:Yingbo Sun
     * 签到配置列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function deployList()
    {
        $redis = Cache::store('redis');
        $redis->select(10);
        $bool = $redis->has("sign:deployList");
        if($bool){
            $data = $redis->get('sign:deployList');
        }else{
            $data = UserSignDeploy::where('is_status',0)->select();
            $data && $data = $data->toArray();
            $redis->set('sign:deployList',$data);
        }
        $res = ['code' => 200,'message' => '','data' => $data];
        return $this->res($res);
    }


}
